//
//  QDUCInfoHeaderView.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/2/22.
//  Copyright © 2019 YueWen. All rights reserved.
//

import UIKit
import SnapKit

protocol QDUCInfoHeaderViewDelegate: class {
    
    /// 进行回调
    @available(iOS, deprecated: 1.0, message: "为了适配swift-router机制，使用infoHeaderView(view:routerKey:)")
    func infoHeaderView(view:QDUCInfoHeaderView,clickInfo:[String:String]?)
    /// 点击进行响应的回调
    func infoHeaderView(view:QDUCInfoHeaderView,routerKey: UIViewController.PlatformKey)
}

extension QDUCInfoHeaderViewDelegate {
    
    func infoHeaderView(view:QDUCInfoHeaderView,clickInfo:[String:String]?) {}
    func infoHeaderView(view:QDUCInfoHeaderView,routerKey: UIViewController.PlatformKey) {}
}



extension QDUCInfoHeaderViewDelegate where Self: UIViewController {
    
//    func infoHeaderView(view:QDUCInfoHeaderView,clickInfo:[String:String]?) {
//        router(byInfo: clickInfo)
//    }
    
    func infoHeaderView(view:QDUCInfoHeaderView,routerKey: UIViewController.PlatformKey) {
        router(platform: routerKey)
    }
}



/// 个人中心信息的headerView
final class QDUCInfoHeaderView: UIView {

    /// 头像
    let headerImageButton = UIImageView()
    /// 用户昵称
    let nickNameLabel = UILabel()
    /// 登陆的标签
    let loginLabel = UILabel()
    /// 编辑个人信息按钮
    let editButton = UIButton()
    /// 积分标签
    let integralLabel = UILabel()
    /// 代理对象
    weak var delegate: QDUCInfoHeaderViewDelegate?
    
    /// 金融模块
    
    /// 金融服务的背景图
//    fileprivate let financeBackgroundImageView = UIImageView()
//    /// 金融服务的图标
//    fileprivate let financeImageView = UIImageView()
//    /// 金融服务的标题
//    fileprivate let financeTitleLabel = UILabel()
//    /// 金融服务的描述(ex:地铁宝充值享福利)
//    fileprivate let financeSubButton = UIButton()
//    /// 金融服务的右箭头
//    fileprivate let financeArrowImageView = UIImageView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .white
        
        
//        headerImageButton.adjustsImageWhenHighlighted = false
        headerImageButton.layer.cornerRadius = 56 / 2
        headerImageButton.clipsToBounds = true
        headerImageButton.contentMode = .scaleAspectFill
        headerImageButton.backgroundColor = QDColors.theme
        headerImageButton.isUserInteractionEnabled = true
        
        nickNameLabel.textColor = QDColors.black
        nickNameLabel.font = Font.semibold.font(size: 22)
        nickNameLabel.text = "137 **** 6666"
        nickNameLabel.isUserInteractionEnabled = true
        
        loginLabel.textColor = QDColors.black
        loginLabel.font = Font.semibold.font(size: 22)
        loginLabel.text = "点击登录"
        loginLabel.isHidden = true
        loginLabel.isUserInteractionEnabled = true
        
        editButton.setTitleColor(#colorLiteral(red: 0.5921568627, green: 0.6, blue: 0.5960784314, alpha: 1), for: .normal)
        editButton.layer.cornerRadius = 12.5
        editButton.layer.borderColor = #colorLiteral(red: 0.862745098, green: 0.8705882353, blue: 0.862745098, alpha: 1)
        editButton.layer.borderWidth = 0.5
        editButton.setTitle("编辑个人信息", for: .normal)
        editButton.adjustsImageWhenHighlighted = false
        editButton.titleLabel?.font = Font.regular.font(size: 12)
        
        integralLabel.textColor = #colorLiteral(red: 0.5921568627, green: 0.6, blue: 0.5960784314, alpha: 1)
        integralLabel.font = Font.regular.font(size: 12)
        integralLabel.text = "积分: 1000"
    
        addSubview(headerImageButton)
        addSubview(nickNameLabel)
        addSubview(loginLabel)
        addSubview(editButton)
        addSubview(integralLabel)
        
        loginLabel.addUIControlHandlerTarget(self, action: #selector(login))
        integralLabel.addUIControlHandlerTarget(self, action: #selector(gotoIntegral))
        headerImageButton.addUIControlHandlerTarget(self, action: #selector(editInformation));
//        headerImageButton.addTarget(self, action: #selector(editInformation), for: .touchUpInside)
        editButton.addTarget(self, action: #selector(editInformation), for: .touchUpInside)
        
        let topSpace = hasPhoneHeader() ? 20 : 0
        
        //布局
        headerImageButton.snp.makeConstraints { (make) in
            make.height.width.equalTo(56).priority(.required)
            make.left.equalToSuperview().offset(20)
            make.top.equalToSuperview().offset(15 + topSpace)
        }

        nickNameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(headerImageButton.snp.right).offset(20)
            make.top.equalToSuperview().offset(13 + topSpace)
            make.height.lessThanOrEqualTo(30)
            make.right.equalToSuperview().inset(20).priority(.high)
        }
        
        loginLabel.snp.makeConstraints { (make) in
            make.left.equalTo(headerImageButton.snp.right).offset(20)
            make.height.lessThanOrEqualTo(30)
            make.right.equalToSuperview().inset(20).priority(.high)
            make.centerY.equalTo(headerImageButton)
        }

        editButton.snp.makeConstraints { (make) in
            make.left.equalTo(nickNameLabel)
            make.top.equalToSuperview().offset(13 + 30 + 4 + topSpace)
            make.height.equalTo(25)
            make.width.equalTo(88)
        }

        integralLabel.snp.makeConstraints { (make) in
            make.left.equalTo(editButton.snp.right).offset(12)
            make.top.equalToSuperview().offset(13 + 30 + 7 + topSpace)
            make.height.equalTo(17)
        }

        
//        //金融模块
//        financeBackgroundImageView.isUserInteractionEnabled = true
//        financeBackgroundImageView.backgroundColor = #colorLiteral(red: 0.4705882353, green: 0.6352941176, blue: 0.9490196078, alpha: 1)
//
//        financeImageView.backgroundColor = .orange
//
//        financeTitleLabel.textColor = .white
//        financeTitleLabel.font = Font.medium.font(size: 17)
//        financeTitleLabel.text = "金融服务"
//
//        financeSubButton.setTitleColor(.white, for: .normal)
//        financeSubButton.setTitle("地铁宝充值享福利", for: .normal)
//        financeSubButton.titleLabel?.font = Font.light.font(size: 10)
//        financeSubButton.titleLabel?.textAlignment = .center
//        financeSubButton.layer.cornerRadius = 10
//        financeSubButton.layer.borderColor = UIColor.white.cgColor
//        financeSubButton.layer.borderWidth = 0.5
//
//        financeArrowImageView.image = "uc_arrow_right_white".namedImage
//
//        addSubview(financeBackgroundImageView)
//        financeBackgroundImageView.addSubview(financeImageView)
//        financeBackgroundImageView.addSubview(financeTitleLabel)
//        financeBackgroundImageView.addSubview(financeSubButton)
//        financeBackgroundImageView.addSubview(financeArrowImageView)
//
//        financeBackgroundImageView.snp.makeConstraints { (make) in
//            make.top.equalToSuperview().offset(149 - 64)
//            make.left.equalToSuperview().offset(13)
//            make.right.equalToSuperview().inset(13).priority(ConstraintPriority.high)
//            make.height.equalTo(78)
//        }
//
//        financeImageView.snp.makeConstraints { (make) in
//            make.left.equalToSuperview().offset(19)
//            make.width.height.equalTo(18)
//            make.centerY.equalToSuperview()
//        }
//
//        financeTitleLabel.snp.makeConstraints { (make) in
//            make.centerY.equalToSuperview()
//            make.left.equalTo(financeImageView.snp.right).offset(5)
//            make.height.equalTo(24)
//        }
//
//        financeArrowImageView.snp.makeConstraints { (make) in
//            make.right.equalToSuperview().inset(22)
//            make.width.height.equalTo(16)
//            make.centerY.equalToSuperview()
//        }
//        
//        financeSubButton.snp.makeConstraints { (make) in
//            make.right.equalToSuperview().inset(46)
//            make.centerY.equalToSuperview()
//            make.height.equalTo(20)
//            make.width.equalTo(98)
//        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    /// 更新用户状态
    func updateInfoStatus(isLogin: Bool){
        
        editButton.isHidden = !isLogin
        integralLabel.isHidden = !isLogin
        
        if isLogin {
            //显示昵称
            nickNameLabel.text = QDUserInfoModel.share.nickName
            nickNameLabel.isHidden = false
            loginLabel.isHidden = true
            integralLabel.text = "积分：\(QDUserInfoModel.share.creditSum)"
            integralLabel.isHidden = false
            headerImageButton.kf.setImage(with: URL(string: QDUserInfoModel.share.image), placeholder: "uc_header".namedImage, options: nil, progressBlock: nil, completionHandler: nil)
            
        } else {
            nickNameLabel.isHidden = true
            loginLabel.isHidden = false
            integralLabel.isHidden = true
            headerImageButton.image = "uc_header".namedImage
        }
    }
    
    /// 进入积分界面
    @objc func gotoIntegral(){
        delegate?.infoHeaderView(view: self, routerKey: .grade)
//        delegate?.infoHeaderView(view: self, clickInfo: [QDGlobalPlatform: QDRouterPlatformGrade])
    }
    
    /// 编辑个人信息
    @objc func editInformation(){
        delegate?.infoHeaderView(view: self, routerKey: .myInfo)
//        delegate?.infoHeaderView(view: self, clickInfo: [QDGlobalPlatform: QDRouterPlatformEditInfo])
    }

    /// 登陆
    @objc func login() {
        if editButton.isHidden == false { return }
        //进行登录
        delegate?.infoHeaderView(view: self, routerKey: .login)
//        delegate?.infoHeaderView(view: self, clickInfo: [QDGlobalPlatform: QDRouterPlatformLogin])
    }
}
